package com.util;

import cn.hutool.core.util.HexUtil;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;

public class DESUtil {

    /**
     * 加密
     * @param datasource byte[]
     * @param password String
     * @return byte[]
     */
    //DES算法
    public static byte[] desCrypto(byte[] datasource, byte[] password) {
        try{
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password);
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            return cipher.doFinal(datasource);
        }catch(Throwable e){
            e.printStackTrace();
        }
        return null;
    }

    //DES算法
    public static byte[] desCryptoECB(byte[] datasource, byte[] password) {
        try{
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password);
            // 创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            // Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            // 用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            // 现在，获取数据并加密
            // 正式执行加密操作
            return cipher.doFinal(datasource);
        }catch(Throwable e){
            e.printStackTrace();
        }
        return null;
    }


    public static void main1(String[] args) {
        String source = "00000000000000000000000000000000" ;
//        String source = "00000000000000000000000000000000";
        System.out.println("原文: " + source);
        String key = "00000000000000000000000000000000";
        // 23129287BB4B0AE4
//        String encryptData1 = encrypt(source, key);
        String encryptData1 = HexUtil.encodeHexStr(desCrypto(HexUtil.decodeHex(source) , HexUtil.decodeHex(key)));
        System.out.println("加密后: 0082000008" + encryptData1.substring(0 , 16).toUpperCase());
        System.out.println("加密后: " + encryptData1.toUpperCase().substring(0 , 32));
//        String dencryptData1 = decrypt(encryptData1,key);
//        System.out.println("解密后: " + dencryptData1);

//        System.out.println(HexUtil.encodeHex("\n".getBytes()));

        //8CA64DE9C1B123A78CA64DE9C1B123A7
    }

    public static void main(String[] args) throws Exception {
        String CSNL = "4F1C7CE912112810" ;
        byte[] csnl_byte = HexUtil.decodeHex(CSNL) ;
        byte[] pcsn_byte = HexUtil.decodeHex("FFFFFFFFFFFFFFFF") ;
        for(int i = 0 ; i < csnl_byte.length ; i++) {
            csnl_byte[i] = (byte) ((csnl_byte[i] ^ pcsn_byte[i]) & 0xff) ;
        }
        System.out.println(HexUtil.encodeHex(csnl_byte));
        String CSN = CSNL + HexUtil.encodeHexStr(csnl_byte).toUpperCase() ;
        System.out.println(CSN);
        byte[] res = ThreeDes.encr(HexUtil.decodeHex(CSN) , HexUtil.decodeHex("46B6A8B059CBB4CBC3EB0DCC4CF72D1C")) ;
        byte[] result = ThreeDes.encr(HexUtil.decodeHex("00000000000000000000000000000000") , res) ;
        System.out.println(HexUtil.encodeHexStr(result).toUpperCase());
        // B0E4F6374E13C8B47CE669138C60CB58
        // edc17b65dca45922406a03a9fe0aff02fa6390e8df536a89
//        System.out.println( ^ HexUtil.decodeHex("FFFFFFFFFFFFFFFF"));
    }
}
